package com.nginxmanager.comment;

import com.nginxmanager.domain.Proxy;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018-01-09.
 */
public class StringTool {
    public Proxy getProxy(String str){
        Proxy proxy = new Proxy();
        String point;
        String weight;
        String fail_timeout;
        String max_fails;
        String[] str1;
        Pattern p = Pattern.compile("(.*)(:)(.*)");
        Matcher m = p.matcher(str);
        if(m.matches()){
            str1=str.split(":");
            proxy.setIp(str1[0].trim());
            proxy.setPort(str1[1].trim());
            p = Pattern.compile("(.*)(weight=)(.*)(max_fails=)(.*)(fail_timeout=)(.*)");
            m = p.matcher(str1[1]);
            if (m.matches()){
                point = m.group(1);
                proxy.setPort(point.trim());
                weight=m.group(3);
                proxy.setWeight(weight.trim());
                fail_timeout=m.group(7);
                proxy.setFail_timeout(fail_timeout.trim());
                max_fails=m.group(5);
                proxy.setMax_fails(max_fails.trim());
                return proxy;
            }else{
                return proxy;
            }
        }else {
            str1 = new String[1];
            p = Pattern.compile("(.*)(weight)(.*)");
            m = p.matcher(str);
            if(m.matches()){
                str1[0] = m.group(1);
                proxy.setIp(str1[0].trim());
                p = Pattern.compile("(.*)(weight=)(.*)(max_fails=)(.*)(fail_timeout=)(.*)");
                m = p.matcher(str);
                if (m.matches()){
                    weight=m.group(3);
                    proxy.setWeight(weight.trim());
                    fail_timeout=m.group(7);
                    proxy.setFail_timeout(fail_timeout.trim());
                    max_fails=m.group(5);
                    proxy.setMax_fails(max_fails.trim());
                }else{
                    return proxy;
                }
            }else{
                proxy.setIp(str);
                return proxy;
            }
        }

        System.out.println("天灵灵地灵灵："+proxy.toString());
        return proxy;
    }
}
